package com.maschinenstuermer.profiler.model.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;

public abstract class CompositeInvocation extends Invocation {
	@XmlElements(value = {
			@XmlElement(name = "methodInvocation", type = MethodInvocation.class),
			@XmlElement(name = "sqlStatementExecution", type = SqlStatementExecution.class),
			@XmlElement(name = "transaction", type = Transaction.class) })
	private List<Invocation> children = new ArrayList<Invocation>();
	private transient Map<String, MethodInvocation> methodInvocationLookup;

	public List<Invocation> getChildren() {
		return children;
	}

	/**
	 * Provides a filter version of this objects children
	 * 
	 * @return a unmodifiable collection containing all method invocation
	 *         children
	 */
	public List<MethodInvocation> getMethodInvocations() {
		final List<MethodInvocation> result = new ArrayList<MethodInvocation>();
		for (Invocation invocation : getChildren()) {
			if (invocation instanceof MethodInvocation) {
				MethodInvocation methodInvocation = (MethodInvocation) invocation;
				result.add(methodInvocation);
			}
		}
		return Collections.unmodifiableList(result);
	}

	/**
	 * Provides a filter version of this objects children
	 * 
	 * @return a unmodifiable collection containing all sql execution statement
	 *         children
	 */
	public List<SqlStatementExecution> getSqlStatementExecutions() {
		final List<SqlStatementExecution> result = new ArrayList<SqlStatementExecution>();
		for (Invocation invocation : getChildren()) {
			if (invocation instanceof SqlStatementExecution) {
				SqlStatementExecution sqlStatementExecution = (SqlStatementExecution) invocation;
				result.add(sqlStatementExecution);
			}
		}
		return Collections.unmodifiableList(result);
	}

	public final MethodInvocation lookupOrCreate(String methodName) {
		if (methodInvocationLookup == null) {
			initMethodLookup();
		}
		MethodInvocation result = methodInvocationLookup.get(methodName);
		if (result == null) {
			result = new MethodInvocation(methodName);
			methodInvocationLookup.put(methodName, result);
			getChildren().add(result);
		}
		return result;
	}

	public void add(Transaction transaction) {
		getChildren().add(transaction);
	}

	public void add(SqlStatementExecution sqlStatementExceution) {
		getChildren().add(sqlStatementExceution);
	}

	private void initMethodLookup() {
		methodInvocationLookup = new HashMap<String, MethodInvocation>();
		for (Invocation invocation : getChildren()) {
			if (invocation instanceof MethodInvocation) {
				final MethodInvocation methodInvocation = (MethodInvocation) invocation;
				String methodName = methodInvocation.getMethodName();
				methodInvocationLookup.put(methodName, methodInvocation);
			}
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((children == null) ? 0 : children.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		CompositeInvocation other = (CompositeInvocation) obj;
		if (children == null) {
			if (other.children != null)
				return false;
		} else if (!children.equals(other.children))
			return false;
		return true;
	}
}
